﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.款式异常;
using DXApplicationTangche.service;
using mendian;
using static mendian.Frm设计点;
using System.IO;
using DiaoPaiDaYin;
using DXApplicationTangche.原型;
using DXApplicationTangche.Utils;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.service.redis_service;
using Seagull.BarTender.Print;
using static DXApplicationTangche.原型.TaskDTOS;
using DXApplicationTangche.DTO;
using DXApplicationTangche.report;
using DevExpress.Xpf.Printing;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.model;
using DevExpress.XtraGrid.Demos.util;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.辅料;
using DXApplicationTangche.UC.套号管理.套号吊牌;
using static DXApplicationTangche.DTO.DTOStyleMeasureItems;
using DXApplicationTangche.UC.套号管理.套号吊牌.service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.生产计划.编排生产批.service;

namespace DXApplicationTangche.UC.门店下单.form.订单修改
{
    public partial class Frm订单预览 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private OrderCommitDTO返回 OrderCommitDTO返回 = new OrderCommitDTO返回();

        private StyleCategoryDTO styleCategoryDTO;
        private StyleAccessoryItemsDTO styleAccessoryItemsDTO;
        public DTOStyleMeasureItems dtoStyleMeasureItems = new DTOStyleMeasureItems();
        private Enum订单类别 @enum;
        public List<ContrastForUCDto> contrastForUCDtos = new List<ContrastForUCDto>();
        public TaskDTOS TaskDTOS = new TaskDTOS();
        public TaskDTO TaskDTO = new TaskDTO();
        private 门店下单选款式Model model;
        private String Style_Id;
        private String ORDER_ID;
        private String REMARKS;
        //private List<尺寸呈现dto> 尺寸呈现 = new List<尺寸呈现dto>();
        private Dictionary<String, String> template_choose = new Dictionary<string, string>();//模板名称和id
        public Frm订单预览(String Style_Id, List<尺寸呈现dto> lst, String ORDER_ID, String REMARKS, Enum订单类别 @enum)
        {
            InitializeComponent();
            this.Style_Id = Style_Id;
            this.ORDER_ID = ORDER_ID;
            this.REMARKS = REMARKS;
            this.@enum = @enum;
            //  订单相关信息
            this.model = new 门店下单选款式Model()
                .build尺寸呈现(lst)
                ;
            //  this.model.build订单Model()
            //  控件行为

            this.gridControl款式.DataSource = this.model.款式图片一览;
            this.gridControl面料.DataSource = this.model.面料信息;
            this.gridControlSize.DataSource = this.model.尺寸呈现;
            this.gridControl设计点.DataSource = this.model.Dto定制下单.Dto设计点s;
            //  模板  TODO
            ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items.Add("样品下单");
            ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items.Add("定制下单");
            this.barEditItemTemplate.EditValue = ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items[0];
            this.订单基本信息呈现();
            this.styleCategoryDTO = FunctionHelper.JsonDeserialization<StyleCategoryDTO>(RestCall.httpGetMethod(URL.接口url + "/style/getCategoryByStyleId?styleId=" + this.model.Dto定制下单.Style_Id));
        }
        public Frm订单预览(String Style_Id, List<尺寸呈现dto> lst, String ORDER_ID, String REMARKS, Enum订单类别 @enum, DTOStyleMeasureItems dTO)
        {
            InitializeComponent();
            this.Style_Id = Style_Id;
            this.ORDER_ID = ORDER_ID;
            this.REMARKS = REMARKS;
            this.@enum = @enum;
            //  订单相关信息
            this.model = new 门店下单选款式Model()
                .build尺寸呈现(lst)
                ;
            //  this.model.build订单Model()
            this.dtoStyleMeasureItems = dTO;
            try
            {
                String json = RestCall.httpGetMethod(URL.接口url + "/barcode/getOrderCacheByOrderId?orderId=" +
                ORDER_ID);
                this.OrderCommitDTO返回 = FunctionHelper.JsonDeserialization<OrderCommitDTO返回>(json);
                this.model.build面料信息(this.OrderCommitDTO返回);

            }
            catch (Exception)
            {

                throw;
            }
            this.dtoStyleMeasureItems.measureItemDTOS = this.OrderCommitDTO返回.measureItemDTOS;
            this.dtoStyleMeasureItems.measureItemDTOS.Sort(new DTOMeasureItemIndexComparer());
            this.treeList尺寸.DataSource = dtoStyleMeasureItems.measureItemDTOS;
            //  控件行为

            this.gridControl款式.DataSource = this.model.款式图片一览;
            this.gridControl面料.DataSource = this.model.面料信息;
            this.gridControlSize.DataSource = this.model.尺寸呈现;
            this.gridControl设计点.DataSource = this.model.Dto定制下单.Dto设计点s;
            //  模板  TODO
            ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items.Add("样品下单");
            ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items.Add("定制下单");
            this.barEditItemTemplate.EditValue = ((DevExpress.XtraEditors.Repository.RepositoryItemComboBox)this.barEditItemTemplate.Edit).Items[0];
            this.订单基本信息呈现();
            this.styleCategoryDTO = FunctionHelper.JsonDeserialization<StyleCategoryDTO>(RestCall.httpGetMethod(URL.接口url + "/style/getCategoryByStyleId?styleId=" + this.model.Dto定制下单.Style_Id));
        }

        private void gridViewSize_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            
        }

        private void gridViewSize_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //e.Appearance.BackColor = Color.NavajoWhite;
            if (e.Column.Name == "colGarment")
            {
                if (this.model.尺寸呈现[e.RowHandle].OUT_VALUE > this.model.尺寸呈现[e.RowHandle].leastReasonable)
                {
                    //该行数据的该列的值不为空时时,其背景色为Red
                    e.Appearance.BackColor = Color.Red;//设置单元格变色
                                                       //e.Column.AppearanceCell.BackColor = Color.Red;//设置数据列变色
                }
                else if (this.model.尺寸呈现[e.RowHandle].IN_VALUE > this.model.尺寸呈现[e.RowHandle].maxReasonable)
                {
                    //该行数据的该列的值不为空时时,其背景色为Red
                    e.Appearance.BackColor = Color.Green;//设置单元格变色
                                                         //e.Column.AppearanceCell.BackColor = Color.Red;//设置数据列变色
                }
                else
                {
                    //e.Appearance.BackColor = Color.LightGray;
                }
            }
        }

        private void tileView1_ItemDoubleClick(object sender, DevExpress.XtraGrid.Views.Tile.TileViewItemClickEventArgs e)
        {
            new Frm设计点(this.model.Dto定制下单.Dto设计点s[this.tileView1.FocusedRowHandle], Enum选择设计点类型.全部, this.Style_Id,this.model.SHOP_ID,this.model.OGNIZATION_ID,this.ORDER_ID).ShowDialog();
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            #region  原来的打印订单纸
            if (this.@enum == Enum订单类别.销售订单 || this.@enum == Enum订单类别.套号订单||this.@enum==Enum订单类别.Luna店铺订单)
            {
                this.model.尺寸呈现.Clear();
                String str = FunctionHelper.JsonSerialization(dtoStyleMeasureItems.measureItemDTOS);
                foreach (MeasureItemDTO dto in dtoStyleMeasureItems.measureItemDTOS)
                {
                    this.model.尺寸呈现.Add(new 尺寸呈现dto(dto));
                }
            }
            Image pic面料 = (Image)this.tileView2.GetRowCellValue(0, "picture");
            //订单导出DTO 订单导出DTO = OrderService.getOrderInfo(this.ORDER_ID, this.model.尺寸呈现, this.model.Dto定制下单.Dto设计点s, pic面料);
            //订单导出DTO.build客户体态新新(this.OrderCommitDTO返回.customerFitRDTOS);
            #endregion

            List<订单导出DTO> 订单导出DTOs = new List<订单导出DTO>();
            //订单导出DTOs.Add(订单导出DTO);
            XtraReport订单 report = new XtraReport订单();
            switch (this.@enum)
            {
                case Enum订单类别.销售订单:
                    break;
                case Enum订单类别.坚山订单:
                    report.坚山颜色水印();
                    break;
                case Enum订单类别.坚山美国订单:
                    report.美国坚山颜色水印();
                    break;
                case Enum订单类别.样品订单:
                    report.样品颜色水印();
                    break;
                case Enum订单类别.套号订单:
                    report.套号颜色水印();
                    break;
                case Enum订单类别.Luna店铺订单:
                    report.Luna颜色水印();
                    break;
            }
            report.DataSource = 订单导出DTOs;
            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
        }
        
        private void 订单基本信息呈现()
        {
            this.textBox订单号.Text = this.model.订单基本信息.ORDER_NO;
            this.textBox尺码.Text = this.model.订单基本信息.尺寸;
            this.textBox交易号.Text = this.model.订单基本信息.BUSINESS_NO;
            this.textBox销售信息姓名.Text = this.model.订单基本信息.销售信息姓名;
            this.textBox销售信息性别.Text = this.model.订单基本信息.销售信息性别;
            this.textBox销售信息电话.Text = this.model.订单基本信息.销售信息电话;
            this.textBox发运信息地址.Text = this.model.订单基本信息.收货人地址;
            this.textBox发运信息姓名.Text = this.model.订单基本信息.收货人姓名;
            this.textBox发运信息电话.Text = this.model.订单基本信息.收货人电话;
            this.richTextBox备注.Text = this.model.Dto定制下单.REMARKS;

            this.gridControl备注图片.DataSource = this.model.Dto定制下单.备注图片s;
        }

        private void richTextBox备注_TextChanged(object sender, EventArgs e)
        {
            OrderService.ChangeOrderRemark(this.ORDER_ID, this.richTextBox备注.Text);
        }

        private void gridView辅料类别_DoubleClick(object sender, EventArgs e)
        {
            foreach (ContrastForUCDto dto in this.contrastForUCDtos)
            {
                if (dto.id.Equals(FunctionHelper.GetgridViewValue(this.gridView辅料类别, this.gridView辅料类别.FocusedRowHandle, "id")))
                {
                    new Frm辅料选择(FunctionHelper.GetgridViewValue(this.gridView辅料类别, this.gridView辅料类别.FocusedRowHandle, "id"), FunctionHelper.GetgridViewValue(this.gridView辅料类别, this.gridView辅料类别.FocusedRowHandle, "code"), FunctionHelper.GetgridViewValue(this.gridView辅料类别, this.gridView辅料类别.FocusedRowHandle, "name"), dto.DtoForChooseContrasts, this.styleCategoryDTO.code).ShowDialog();
                    break;
                }
            }
            this.styleAccessoryItemsDTO = new StyleAccessoryItemsDTO(this.model.Dto定制下单.STYLE_NAME_CN, this.contrastForUCDtos).buildOrderid(this.ORDER_ID);
            String Json辅料保存 = FunctionHelper.JsonSerialization(this.styleAccessoryItemsDTO.styleAccessoryDetails);
            String log = RestCall.PostHttp(URL.接口url + "/acc/saveStyleAccessoryItems?orderId=" + styleAccessoryItemsDTO.orderId + "&styleName=" + styleAccessoryItemsDTO.styleName, Json辅料保存);
            try
            {
                this.contrastForUCDtos = FunctionHelper.JsonDeserialization<List<ContrastForUCDto>>(RestCall.httpGetMethod(URL.接口url + "/acc/getStyleAccessoryItemsByStyleId?orderId=" + this.ORDER_ID));
                this.gridControl辅料信息.DataSource = this.contrastForUCDtos;
            }
            catch { }
            this.gridView辅料类别.RefreshData();
        }

        private void treeList尺寸_CellValueChanged(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            try
            {
                this.dtoStyleMeasureItems.build更改尺寸(
                    this.treeList尺寸.FocusedNode["id"].ToString(),
                    e.Column.FieldName,
                    e.Value.ToString(),
                    this.Style_Id,
                    this.ORDER_ID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.treeList尺寸.DataSource = this.dtoStyleMeasureItems.measureItemDTOS;
            this.treeList尺寸.Refresh();
        }
    }
}